﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using STS.Interfaces;
using System.Collections;
using STS.Schema.Base;
using STS.Schema.Interfaces;

namespace STS.Schema
{
    public static class SchemaUtils
    {
        public static PropertyInfo GetSqlPropertyInfo(object instance, string sqlColumnName)
        {
            PropertyInfo[] infoList = instance.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
            foreach (PropertyInfo info in infoList)
            {
                SqlQueryPropertyAttribute sqlProp = GetAttribute<SqlQueryPropertyAttribute>(info);
                if (sqlProp != null && sqlProp.SqlQueryColumn == sqlColumnName)
                {
                    return info;
                }
            }
            return null;
        }

        internal static T GetAttribute<T>(PropertyInfo property) where T : Attribute
        {
            object[] attributeList = property.GetCustomAttributes(true);
            if (attributeList == null)
            {
                return null;
            }
            foreach (object attribute in attributeList)
            {
                if (attribute.GetType() == typeof(T))
                {
                    return attribute as T;
                }
            }
            return null;
        }

        //public static TCollection ToGenericCollection<TCollection, T>(this IEnumerable source)
        //   // where T : ISqlObject
        //    where TCollection : ICollection, IEnumerable
        //{

        //    foreach (object item in source)
        //    {
        //        if (item is T)
        //        {
        //            yield return (T)item;
        //        }
        //    } 
        //    TCollection list = (TCollection)source.OfType<T>();
        //    //foreach (T item in source)
        //    //{
        //    //    yield return 
        //    //}
        //    return list;
        //}
    }
}
